﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace App
{
    public partial class FrmMain : Form
    {
        public const int LIST_MENU_IMAGE_SIZE = 64;
        public const int LIST_MENU_ITEM_PADDING = 5;
        public const int LIST_MENU_ITEM_SPACING = 2;

        // private List<Form> _lst_forms;

        public FrmMain()
        {
            InitializeComponent();
            // _lst_forms = new List<Form>();
        }

        private void SetActivateForm(Form form)
        {
            if (pnContent.Controls.Count > 0)
            {
                pnContent.Controls[0].Dispose();
                pnContent.Controls.Clear();
            }

            form.TopLevel = false;

            if (form.GetType() != typeof(FrmQLDanhMuc))
            {
                form.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
                form.Dock = DockStyle.Fill;
            }

            form.Visible = true;
            pnContent.Controls.Add(form);
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            /* _lst_forms.Add(new FrmThongTin());
            _lst_forms.Add(new FrmQLDanhMuc());
            _lst_forms.Add(new FrmQLSanPham());
            _lst_forms.Add(new FrmDonHang());
            _lst_forms.Add(new FrmNhapHang());
            _lst_forms.Add(new FrmThongKe()); */

            lstMenu.Items.Add(new ListMenuItem(Properties.Resources.img_sys_info, "Thông tin"));
            lstMenu.Items.Add(new ListMenuItem(Properties.Resources.img_category, "Danh mục"));
            lstMenu.Items.Add(new ListMenuItem(Properties.Resources.img_product, "Sản phẩm"));
            lstMenu.Items.Add(new ListMenuItem(Properties.Resources.img_order, "Đơn hàng"));
            lstMenu.Items.Add(new ListMenuItem(Properties.Resources.img_import, "Nhập hàng"));
            lstMenu.Items.Add(new ListMenuItem(Properties.Resources.img_stat, "Thống kê"));
            lstMenu.SetSelected(0, true);
        }

        private void lstMenu_MeasureItem(object sender, MeasureItemEventArgs e)
        {
            ListBox lst = sender as ListBox;
            ListMenuItem item = (ListMenuItem)lst.Items[e.Index];

            e.ItemWidth = LIST_MENU_IMAGE_SIZE + LIST_MENU_ITEM_PADDING * 2;
            e.ItemHeight = LIST_MENU_IMAGE_SIZE + (LIST_MENU_ITEM_PADDING) * 2 + LIST_MENU_ITEM_SPACING +
                (int) Math.Round(e.Graphics.MeasureString(item._title, lst.Font).Height);
        }

        private void lstMenu_DrawItem(object sender, DrawItemEventArgs e)
        {
            ListBox lst = sender as ListBox;
            ListMenuItem item = (ListMenuItem)lst.Items[e.Index];

            e.DrawBackground();
            e.Graphics.DrawImage(item._image, 
                LIST_MENU_ITEM_PADDING, 
                e.Bounds.Top + LIST_MENU_ITEM_PADDING, 
                LIST_MENU_IMAGE_SIZE, 
                LIST_MENU_IMAGE_SIZE);

            Brush br;
            if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) br = SystemBrushes.HighlightText;
            else br = new SolidBrush(e.ForeColor);
            RectangleF rec_title = new RectangleF(
                LIST_MENU_ITEM_SPACING, 
                e.Bounds.Top + LIST_MENU_ITEM_PADDING + LIST_MENU_IMAGE_SIZE + LIST_MENU_ITEM_SPACING,
                (lst.Size.Width - LIST_MENU_ITEM_SPACING * 2),
                e.Graphics.MeasureString(item._title, lst.Font).Height
            );

            StringFormat format = new StringFormat();
            format.LineAlignment = StringAlignment.Center;
            format.Alignment = StringAlignment.Center;

            Font f = new Font(lst.Font, FontStyle.Bold);
            e.Graphics.DrawString(item._title, f, br, rec_title, format);
        }

        private void lstMenu_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListBox lst = sender as ListBox;
            Form frm = null;

            switch (lst.SelectedIndex)
            {
                case 0:
                    frm = new FrmThongTin();
                    break;

                case 1:
                    frm = new FrmQLDanhMuc();
                    break;

                case 2:
                    frm = new FrmQLSanPham();
                    break;

                case 3:
                    frm = new FrmDonHang();
                    break;

                case 4:
                    frm = new FrmNhapHang();
                    break;

                case 5:
                    frm = new FrmThongKe();
                    break;

            }
            SetActivateForm(frm);
        }
    }

    public class ListMenuItem
    {
        public Image _image;
        public String _title;

        public ListMenuItem(Image img, String title)
        {
            _image = img;
            _title = title;
        }
    }
}
